package com.haoshuang.zhouzhoubang.ui.page.welfare.insure

import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityInsureWelfareActivityBinding
import com.haoshuang.zhouzhoubang.model.bean.FreeOrderWelfareBean
import com.haoshuang.zhouzhoubang.model.bean.InsureUseRecordBean
import com.haoshuang.zhouzhoubang.model.bean.ShopWelfareRecordBean
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.page.welfare.free.FreeOrderWelfareAdapter
import com.haoshuang.zhouzhoubang.ui.page.welfare.insure.apply.InsureApplyActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.insure.record.InsureApplyRecordActivity
import com.haoshuang.zhouzhoubang.ui.page.welfare.shop.ShopWelfareRecordAdapter
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setEmptyData
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.vertical

/**
 *  保险使用记录
 */
class InsureWelfareActivityActivity : BaseVMActivity<InsureWelfareActivityViewModel, ActivityInsureWelfareActivityBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<InsureWelfareActivityActivity>()
        }
    }
    // 福利id
    private val id by lazy {
        intent.getIntExtra("id", 0)
    }
    // 总数量
    private val total by lazy {
        intent.getIntExtra("total", 0)
    }

    // 剩余数量
    private var surplus = 0

    private val data = arrayListOf<InsureUseRecordBean>()
    private val adapter = InsureWelfareAdapter(data)

    private val shopData = arrayListOf<ShopWelfareRecordBean>()
    private val shopAdapter = ShopWelfareRecordAdapter(shopData)
    // 记录类型  0: 平台使用记录  1: 商城使用记录
    private var recordType = 0
    override fun initIntent() {
        surplus = intent.getIntExtra("surplus", 0)
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "保险"
        binding.titleBar.leftImageView().click { finish() }
        binding.tvPlatformRecords.isSelected = true
        binding.tvShopRecords.isSelected = false

        binding.tvPlatformRecords.clickWithTrigger {
            binding.tvPlatformRecords.isSelected = true
            binding.tvShopRecords.isSelected = false
            recordType = 0
            binding.refreshLayout.autoRefresh()
            binding.recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
        }

        binding.tvShopRecords.clickWithTrigger {
            binding.tvPlatformRecords.isSelected = false
            binding.tvShopRecords.isSelected = true
            recordType = 1
            binding.refreshLayout.autoRefresh()
            binding.recyclerView.vertical().adapter = shopAdapter
            shopAdapter.setEmptyData()
        }
        binding.apply {
            tvTotalNum.text = total.toString()
            tvSurplusNum.text = surplus.toString()



            recyclerView.vertical().adapter = adapter
            adapter.setEmptyData()
            refreshLayout.setOnRefreshListener {
                p = 1
                isShowing = false
                if(recordType == 0) {
                    vm.getInsureUseRecord(p)
                }else{
                    vm.getGoodsWelfareLog(id, p)
                }
            }
            refreshLayout.setOnLoadMoreListener {
                p++
                isShowing = false
                if(recordType == 0) {
                    vm.getInsureUseRecord(p)
                }else{
                    vm.getGoodsWelfareLog(id, p)
                }
            }

            tvApplyRecord.click {
                // 申请记录
                startPage<InsureApplyRecordActivity>()
            }

            tvApply.clickWithTrigger {
                // 申请保险
                if (surplus == 0) {
                    XPopup.Builder(mActivity)
                        .asCustom(ConfirmDialog(
                            context =mActivity,
                            title = "温馨提示",
                            content = "当前平台赠送保险数量已用完,\n需增加请联系客服咨询.",
                            cancelStr = "取消",
                            confirmStr = "联系客服",
                            isHideCancel = false,
                            onCancel = {},
                            onConfirm = {

                            }))
                        .show()
                    return@clickWithTrigger
                }
                startPageForResult<InsureApplyActivity>("surplus" to surplus){code, data ->
                    if(code == 200 && data != null){
                        toast("申请成功")
                        val insure_count = data.getIntExtra("insure_count", 0)
                        surplus -= insure_count
                        tvSurplusNum.text = surplus.toString()
                        binding.refreshLayout.autoRefresh()
                    }
                }
            }
        }
    }

    override fun initData() {
        binding.refreshLayout.autoRefresh()
    }

    override fun startObserve() {
        vm.getInsureUseRecordData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(data, adapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, data, adapter, p)
            }
        }

        vm.getGoodsWelfareLogData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                binding.refreshLayout.setupEmptyData(shopData, shopAdapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it, shopData, shopAdapter, p)
            }
        }
    }

}